package com.zhu.service.edu.controller.Front;

import com.zhu.commons.utils.R;
import com.zhu.service.edu.mapper.EduChapterMapper;
import com.zhu.service.edu.pojo.EduCourse;
import com.zhu.service.edu.pojo.dto.CourseVo;
import com.zhu.service.edu.pojo.vo.ChapterVo;
import com.zhu.service.edu.pojo.vo.CourseFrontQueryVo;
import com.zhu.service.edu.pojo.vo.SubjectInfoFrontVo;
import com.zhu.service.edu.service.IEduChapterService;
import com.zhu.service.edu.service.IEduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/front/course")
@Api(tags = "课程前端api")
//@CrossOrigin
public class CourseFrontApi {
    @Autowired
    private IEduCourseService courseService;

    @PostMapping("/list")
    @ApiOperation(value = "查询课程列表")
    public R pageList(CourseFrontQueryVo courseFrontQueryVo){
     List<EduCourse> courseList= courseService.selectFrontListS(courseFrontQueryVo);
     if(courseList!=null){
         return R.ok().data("courseList",courseList);
     }
     return R.error().message("课程不存在！");
    }
@Autowired
private IEduChapterService chapterService;
    @GetMapping("/findCourseIntro/{id}")
    @ApiOperation(value = "查询课程详细信息（线程池）")
    public R findCourseIntro(@PathVariable(value = "id") String id){
     List<SubjectInfoFrontVo>  subjectInfoFrontVoList=  courseService.findCourseIntro(id);
        Map<String,Object> map=new LinkedHashMap<>();
     subjectInfoFrontVoList.forEach(courseVo->{
         map.put("courseVo",courseVo);
     });
        /**
         * 查询课程嵌套的课时列表
         */
        List<ChapterVo> chapterVoList = chapterService.nestedList(id);

        if(subjectInfoFrontVoList!=null){
            return R.ok().data("item",map).data("items",chapterVoList);
        }
        return R.error().message("课程不存在！");
    }

    @GetMapping("/findCourseIntro2/{id}")
    @ApiOperation(value = "查询课程详细信息（非线程池）")
    public R findCourseIntro2(@PathVariable(value = "id") String id){
        List<SubjectInfoFrontVo>  subjectInfoFrontVoList=  courseService.findCourseIntro2(id);
        if(subjectInfoFrontVoList!=null){
            return R.ok().data("items",subjectInfoFrontVoList);
        }
        return R.error().message("课程不存在！");
    }

    @ApiOperation("根据课程id查询课程信息")
    @GetMapping("/order/getCourseInfo/{courseId}")
    public CourseVo getCourseInfo(@PathVariable String courseId){
      CourseVo courseVo= courseService.getCourseDtoById(courseId);
      return courseVo;
    }
}
